package edu.nf.ch06.exception;

import edu.nf.ch06.web.vo.ResultVO;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author wangl
 * @date 2025/5/13
 * 全局异常处理类
 */
//@ControllerAdvice
//如果是RestController，那么就要使用@RestControllerAdvice
@RestControllerAdvice(basePackages = "edu.nf.ch06.web")
public class GlobalExceptionAdvice {

    /**
     * 处理最顶层的自定会异常
     * @param e
     * @return
     */
    @ExceptionHandler(GlobalException.class)
    public <T> ResultVO<T> handleGlobalException(GlobalException e) {
         e.printStackTrace();
         ResultVO<T> vo = new ResultVO<>();
         vo.setCode(e.getErrorCode());
         vo.setMessage(e.getMessage());
         return vo;
    }

    /**
     * 捕获一个最大的异常兜底
     * @param e
     * @return
     * @param <T>
     */
    @ExceptionHandler(Exception.class)
    public <T> ResultVO<T> handleException(Exception e) {
        e.printStackTrace();
        ResultVO<T> vo = new ResultVO<>();
        vo.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        vo.setMessage("服务器繁忙，请稍后尝试");
        return vo;
    }
}